import { useAnnouncementsStore } from '@/stores/announcements.js'
import axios from 'axios'
import { getCurrentInstance } from 'vue'
let $map = null
let draw = null
export const useAnnouncementHook = () => {
  const Store = useAnnouncementsStore()
  const { proxy } = getCurrentInstance()
  /* 公告发布 */
  const handleAnnocementAdd = () => {
    //创建线画笔

    const layer = new ol.layer.Vector({
      source: Store.source1
    })

    $map = proxy.$map
    $map.addLayer(layer)
    /* 2、创建线的画笔 */
    let draw = new ol.interaction.Draw({
      // 绘制的图层
      source: Store.source1,
      // 绘制的类型
      type: 'Circle'
      // type: 'LineString'
    })
    /* 3、添加交互 */
    $map.addInteraction(draw)

    /* 监听画笔 */
    draw.on('drawend', (e) => {
      //创建缓冲区
      // const line = turf.lineString(e.feature.getGeometry().getCoordinates())
      // // 创建线的缓冲区，第二个参数是缓冲区的半径（单位是度）
      // const buffer = turf.buffer(line, 0.1)
      // // 打印结果
      // console.log(buffer.geometry.coordinates)
      // // 绘制缓冲区
      // const Polygon = new ol.Feature({
      //   geometry: new ol.geom.Polygon(buffer.geometry.coordinates)
      // })
      // /* 2、设置去样式信息 */
      // //设置区样式信息
      // Polygon.setStyle(
      //   new ol.style.Style({
      //     //填充色
      //     fill: new ol.style.Fill({
      //       color: 'black'
      //     }),
      //     //边线颜色
      //     stroke: new ol.style.Stroke({
      //       color: '#ffcc33',
      //       width: 2
      //     })
      //   })
      // )
      // //添加到图层中
      // Store.source1.addFeature(Polygon)
      // var vector = new ol.layer.Vector({
      //   source: Store.source1
      // })
      // /* 5、将图层添加到地图容器中 */
      // $map.addLayer(vector)
      //初始化查询结构对象，设置查询结构包含几何信息
      var queryStruct = new Zondy.Service.QueryFeatureStruct()
      //是否包含几何图形信息
      queryStruct.IncludeGeometry = true
      //是否包含属性信息
      queryStruct.IncludeAttribute = true
      //是否包含图形显示参数
      queryStruct.IncludeWebGraphic = false
      //创建一个用于查询的圆
      var geomObj = new Zondy.Object.Circle()
      // var geomObj = new Zondy.Object.Polygon()
      geomObj.setByOL(e.feature.values_.geometry)

      // geomObj.setByOL(Polygon.getGeometry())
      //指定查询规则
      var rule = new Zondy.Service.QueryFeatureRule({
        //是否将要素的可见性计算在内
        EnableDisplayCondition: false,
        //是否完全包含
        MustInside: false,
        //是否仅比较要素的外包矩形
        CompareRectOnly: false,
        //是否相交
        Intersect: true
      })
      //实例化查询参数对象
      var queryParam = new Zondy.Service.QueryParameter({
        geometry: geomObj,
        resultFormat: 'json',
        struct: queryStruct,
        rule: rule
      })
      //设置查询分页号
      queryParam.pageIndex = 0
      //设置查询要素数目
      queryParam.recordNumber = 1000
      //实例化地图文档查询服务对象
      var queryService = new Zondy.Service.QueryDocFeature(queryParam, 'guanggu', 0, {})
      //执行查询操作，querySuccess为查询回调函数
      queryService.query(querySuccess)
      //关闭画笔
      $map.removeInteraction(draw)
    })
  }

  function querySuccess(data) {
    console.log(data.SFEleArray)
    //获取区域名称和物业电话
    const res = data.SFEleArray.map((item) => {
      return item.AttValue
    })
    console.log(res)
    res.forEach((item) => {
      const obj = {}
      obj.Area = item[3]
      obj.tel = item[4]
      Store.AnnForm.push(obj)
    })
    console.log(Store.AnnForm)
    Store.dialogFormVisible = true
    $map.removeInteraction(draw)
  }

  const handleAnnocementAdmin = () => {
    // 获取公告列表
    axios.get('/api/announcements').then((res) => {
      console.log(res.data.result)
      Store.gridData = res.data.result
    })
    Store.dialogTableVisible = true
  }

  /* 公告列表 */
  const handleAnnouncementBoard = () => {
    // 获取公告列表
    axios.get('/api/announcements').then((res) => {
      console.log(res.data.result)
      Store.gridData2 = res.data.result
    })
    Store.dialogBoardVisible = true
  }

  return { handleAnnocementAdd, handleAnnocementAdmin, handleAnnouncementBoard }
}
